const User = require("../model/userModel");

class UserService {
  async createUser(user_name, password) {
    // console.log('Creating user with:', user_name, password); // 添加日志
    const res = await User.create({ user_name, password });
    console.log(res);
    return res;
  }

  async getUserInfo({ id, user_name, password, is_admin }) {
    const whereOpt = {};
    id && Object.assign(whereOpt, { id });
    user_name && Object.assign(whereOpt, { user_name });
    password && Object.assign(whereOpt, { password });
    is_admin && Object.assign(whereOpt, { is_admin });

    const res = await User.findOne({
      attributes: ["id", "user_name", "password", "is_admin"],
      where: whereOpt,
    });
    return res ? res.dataValues : null;
  }

  async updateById({ id, password }) {
    const whereOpt = { id };
    const newUser = {};
    if (password) {
      newUser.password = password;
    }
    const res = await User.update(newUser, { where: whereOpt });
    return res[0] > 0;
  }
}

module.exports = new UserService();
